// pages/get_ticket/index.js
import { getCouponList, getCoupon, getUserCoupon } from '../../api/coupon.js'
import { removeObj } from '../../utils/removeObj.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showToastState:true,//是否已加载全部
    pageNum: 1,
    pageSize: 8,
    currentTab: 0,
    navList: ['待领取', '待使用', '已使用', '已失效'],
    lists: [],
    is_request: false,
    couponRule:'',
    showModal:false,
    content:'',
    loading: false, //上拉加载更多的loading
    refreshLoading: false, //下拉刷新页面的loading
    timestamp:'' // 因为点击后会触发swiper的切换事件，所以用此参数来做事件判断
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.bindChange({ detail: {current:this.data.currentTab } })
    app._getSettingInfo(data=>{
      this.setData({ couponRule: data.couponRule })
    })
  },
  //监听swiper
  bindChange(e) {
    let current = e.detail.current!=null ? e.detail.current:e.currentTarget.dataset.currenttab;
    this.setData({is_request: false,currentTab: current, pageNum: 1,showToastState:true})
    //current 1=待使用；2=已使用；3=已失效
    if ((new Date()).valueOf() - this.data.timestamp > 100 || this.data.timestamp==''){
      this.setData({ timestamp: (new Date()).valueOf()})
      this.getMore()
    }
  },
  //获取数据
  getMore() {
    if (this.data.currentTab == 0) {
      this._getCouponList(this.data.currentTab)
    } else {
      this._getUserCoupon(this.data.currentTab)
    }
  },
  initList() {
    this.setData({refreshLoading: true})
    this.bindChange({ detail: { current: this.data.currentTab } })
  },
  seeRule(e){
    let { lists} =this.data
    let index = e.currentTarget.dataset.index;
    this.setData({ ['lists[' + index + '].showCouponRule']: !lists[index].showCouponRule})
  },
  // 获取优惠券列表
  _getCouponList(index) {
    if (!this.data.is_request) {
      this.setData({is_request: true})
      let { pageNum, pageSize} = this.data
      getCouponList({ pageNum, pageSize }).then(res => {
        this.setListData(res)
      })
    }
  },
  _getUserCoupon(state) {
    if (!this.data.is_request) {
      this.setData({is_request: true})
      let { pageNum, pageSize } = this.data
      let obj = {
        status: state,
        pageNum: pageNum,
        pageSize: pageSize
      }
      if(this.data.showToastState){
        getUserCoupon(obj).then(res => {
          this.setListData(res)
        })
      }
    }
  },
  setListData(res){ // 设置列表数据
    let { pageNum, lists, pageSize } = this.data
    if (res.rows.length > 0) {
      let arr = res.rows;
      arr.forEach(e=>{
        e.showCouponRule=false
      })
      this.setData({
        lists: [...(pageNum == 1 ? [] : lists), ...removeObj(arr, ['searchValue', 'createBy', 'createTime', 'updateBy', 'updateTime', 'params', 'range', 'createNum', 'sendNum'])],
        pageNum: pageNum + 1
      })
    } else if (pageNum==1){
      this.setData({lists: []})
    }
    this.setData({ showToastState: res.rows.length > 0 ? lists.length != res.total && pageSize <= res.total : true, is_request: false, refreshLoading: false })
  },
  gotoBuy() {
    wx.switchTab({url: '/pages/apply/index'})
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //立即领取
  _getCoupon(e) {
    let d = e.currentTarget.dataset
    getCoupon(d.id).then(res => {
      if (res.code == 0) {
        let arr = this.data.lists;
        arr.splice(d.index, 1)
        this.setData({lists: arr})
        wx.showModal({
          title: '',
          content: '领取成功',
          showCancel: false,
          confirmColor: "#576B95",
          confirmText: '好的',
          success(res) {}
        })
      } else {
        wx.showToast({
          title: '领取失败',
          icon: 'none'
        })
      }
    })
  }
})